import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import eslintPlugin from 'vite-plugin-eslint';
import { resolve } from 'path';
import viteCompression from 'vite-plugin-compression';
import requireTransform from 'vite-plugin-require-transform';
import OptimizationPersist from 'vite-plugin-optimize-persist';
import PkgConfig from 'vite-plugin-package-config';
const fs = require('fs');
const path = require('path');

export default defineConfig(({ command, mode }) => 
{
    loadEnv(mode, process.cwd());
    return {
        base: '../',
        define: {
            'process.env': {
                // 'VUE_APP_API_URL':'http://192.168.1.7:5001'
            }
        },
        plugins: [
            vue(),
            PkgConfig(),
            OptimizationPersist(),
            requireTransform({
                fileRegex:/.ts$|.js$|.vue$/
            }),
            eslintPlugin({
                include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue', 'src/**/*.ts'],
                exclude: ['./node_modules/**', './src/components/xpCalendars/utils/**'],
                cache: false
            }),
            viteCompression({
                verbose: true,
                disable: false,
                threshold: 10240,
                algorithm: 'gzip',
                ext: '.gz'
            })
              
        ],
        resolve: {
            alias:{
                '@': resolve(__dirname, 'src'),
                '$': resolve(__dirname, 'packages')
            }
        },
        css: {
            // css预处理器
            preprocessorOptions: {
                scss: {
                    additionalData: `@use './src/style/element.scss' as *;`
                }
            }
        },
        build: {
            target: 'modules',
            outDir: 'dist',
            assetsDir: 'assets',
            minify: 'terser',
            chunkSizeWarningLimit: 1500,
            rollupOptions: {
                output: {
                    chunkFileNames: 'static/js/[name]-[hash].js',
                    entryFileNames: 'static/js/[name]-[hash].js',
                    assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
                }
            },
            commonjsOptions: {
                include: []
            },
            // 清除打印和debug
            terserOptions: {
                compress: {
                    drop_console: true,
                    drop_debugger: true
                }
            }
        },
        server: {
            host: '0.0.0.0',
            port: 8087,
            cors: true,
            open: true,
            hmr: true,
            proxy: {
                // 代理
                '/xp/api': {
                    target:'http://localhost:5005',
                    changeOrigin: true
                    // rewrite: (path) => path.replace(/^\/api/, '')
                }
            }
        }
    };
});
